<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件上传</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
   <div class="addBrand-container" id="food-add-app">
    <div class="container">
        <el-upload class="avatar-uploader"
                action="/common/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeUpload"
                ref="upload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar"></img>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/index.js"></script>
    <script>
      new Vue({
        el: '#food-add-app',
        data() {
          return {
            imageUrl: 'https://chengduheima57.oss-cn-chengdu.aliyuncs.com/2022/07/12/hehe.jpg'
          }
        },
      //
        methods: {
            //response:就是R对象
          handleAvatarSuccess (response, file, fileList) {
              //       this.imageUrl =     /common/download?name=fgjhfjhfxg.jpg
              this.imageUrl = `/common/download?name=${response.data}`
          },
        //前处理：文件是不是图片  文件大小 < 2M
          beforeUpload (file) {
            if(file){
              const suffix = file.name.split('.')[1]
              const size = file.size / 1024 / 1024 < 2
              if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                this.$refs.upload.clearFiles()
                return false
              }
              if(!size){
                this.$message.error('上传文件大小不能超过 2MB!')
                return false
              }
              return file
            }
          }
        }
      })
    </script>
</body>
</html>
<!--
[{
	"code": 1,
	"msg": null,
	"data": [{
		"id": "1397844263642378242",
		"type": 1,
		"name": "湘菜",
		"sort": 1,
		"createTime": "2021-05-27 09:16:58",
		"updateTime": "2021-07-15 20:25:23",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1397844303408574465",
		"type": 1,
		"name": "川菜",
		"sort": 2,
		"createTime": "2021-05-27 09:17:07",
		"updateTime": "2021-06-02 14:27:22",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1397844391040167938",
		"type": 1,
		"name": "粤菜",
		"sort": 3,
		"createTime": "2021-05-27 09:17:28",
		"updateTime": "2021-07-09 14:37:13",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1413341197421846529",
		"type": 1,
		"name": "饮品",
		"sort": 11,
		"createTime": "2021-07-09 11:36:15",
		"updateTime": "2021-07-09 14:39:15",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1413342269393674242",
		"type": 2,
		"name": "商务套餐",
		"sort": 5,
		"createTime": "2021-07-09 11:40:30",
		"updateTime": "2021-07-09 14:43:45",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1413384954989060097",
		"type": 1,
		"name": "主食",
		"sort": 12,
		"createTime": "2021-07-09 14:30:07",
		"updateTime": "2021-07-09 14:39:19",
		"createUser": "1",
		"updateUser": "1"
	}, {
		"id": "1413386191767674881",
		"type": 2,
		"name": "儿童套餐",
		"sort": 6,
		"createTime": "2021-07-09 14:35:02",
		"updateTime": "2021-07-09 14:39:05",
		"createUser": "1",
		"updateUser": "1"
	}],
	"map": {}
}, {
	"code": 1,
	"msg": null,
	"data": [],
	"map": {}
}]


-->